package com.hzhr.iot.device.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.device.sdk.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.sdk.model.flowMeter.FlowMeterDeleteModel;
import com.hzhr.iot.device.sdk.model.flowMeter.FlowMeterDeleteResponseModel;
import com.hzhr.iot.device.sdk.model.flowMeter.FlowMeterPostModel;
import com.hzhr.iot.device.sdk.model.flowMeter.FlowMeterPostResponseModel;
import com.hzhr.iot.device.sdk.model.flowMeter.FlowMeterPutModel;
import com.hzhr.iot.device.sdk.model.flowMeter.FlowMeterPutResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Device_FlowMeterService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 修改流量计 <br/><br/>
     * 简介 : 物联网设备公司现场实施人员修改流量计和所属DTU的信息 <br/>
     * 步骤 : <br/>
     * 1. 查询流量计：根据请求体“流量计模型(修改)”的“流量计编号”查询流量计“流量计_1”， <br/>
     *  如果“流量计_1”不存在，抛出异常“流量计不存在”；(√) <br/>
     * 2. 查询DTU：根据“流量计_1”的“所属物联网设备编号”查询“DTU_1”， <br/>
     *  如果“DTU_1”不存在，抛出异常“DTU不存在”；(√) <br/>
     * 3. 验证新的流量计代码：如果“流量计模型(修改)”的“流量计代码”和“流量计_1”的“代码”不同， <br/>
     *  则验证“流量计模型(修改)”的“流量计代码”在“流量计模型(修改)”的“所属公司编号”所对应的公司下是否存在， <br/>
     *  如果存在，抛出异常“流量计代码已存在”；(√) <br/>
     * 4. 验证新的DTU代码：如果“流量计模型(修改)”的“DTU代码”和“DTU_1”的“代码”不同， <br/>
     *  则“流量计模型(修改)”的“DTU代码”是否存在，如果存在，抛出异常“DTU代码已存在”；(√) <br/>
     * 5. 验证新的所属燃气用户：如果“流量计模型(修改)”的“所属燃气用户代码”和“流量计_1”的“所属燃气用户代码”不同， <br/>
     *  则根据“流量计模型(修改)”的“所属燃气用户代码”查询“流量计模型(修改)”的“所属部门编号”所对应的部门的管辖范围内的燃气用户“燃气用户_1”， <br/>
     *  如果“燃气用户_1”不存在，抛出异常“燃气用户不存在”；(√) <br/>
     * 6. 更新流量计及DTU信息：根据“流量计模型(修改)”更新“流量计_1”和“DTU_1”的相关信息， <br/>
     *  如果“流量计模型(修改)”的“燃气用户代码”和“流量计_1”的燃气用户代码不同，则需关联“流量计_1”和“燃气用户_1”; (√) <br/>
     * 7. 返回信息：生成“流量计响应模型(修改)”并返回；(√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see FlowMeterPutModel body <br/>
     * @see FlowMeterPutResponseModel result <br/>
     */ 
    public FlowMeterPutResponseModel put(Map<String, String> header, Map<String, Object> query, FlowMeterPutModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<FlowMeterPutResponseModel> result = restTemplate.exchange("http://device-service/flow-meters?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.PUT, httpEntity, FlowMeterPutResponseModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("FLOWMETERSERVICE_PUT_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("FLOWMETERSERVICE_PUT_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 删除流量计 <br/><br/>
     * 简介 : 物联网设备公司现场实施人员删除流量计和所属的DTU <br/>
     * 步骤 : <br/>
     * 1. 查询流量计：根据流量计编号查询“流量计_1”，如“流量计_1”不存在，抛出异常“流量计不存在”; (√) <br/>
     * 2. 查询DTU：根据“流量计_1”的DTU编号查询“DTU_1”，如果“DTU_1”不存在，抛出异常“DTU不存在”; (√) <br/>
     * 3. 删除流量计：删除“流量计_1”; (√) <br/>
     * 4. 删除DTU：删除“DTU_1”; (√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see FlowMeterDeleteModel body <br/>
     * @see FlowMeterDeleteResponseModel result <br/>
     */ 
    public FlowMeterDeleteResponseModel delete(Map<String, String> header, Map<String, Object> query, FlowMeterDeleteModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<FlowMeterDeleteResponseModel> result = restTemplate.exchange("http://device-service/flow-meters?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.DELETE, httpEntity, FlowMeterDeleteResponseModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("FLOWMETERSERVICE_DELETE_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("FLOWMETERSERVICE_DELETE_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
    /**
     * 创建流量计 <br/><br/>
     * 简介 : 物联网设备公司现场实施人员创建流量计和DTU，并将该流量计和相应的DTU及燃气用户进行关联 <br/>
     * 步骤 : <br/>
     * 1. 查询燃气用户：根据请求体“流量计模型(新增)”的“流量计代码”和“所属部门编号”查询燃气用户“燃气用户_1”， <br/>
     *  如果“燃气用户_1”不存在，抛出异常“燃气用户不存在”；(√) <br/>
     * 2. 查询DTU：根据“流量计模型(新增)”的“DTU代码”查询DTU“DTU_1”；(√) <br/>
     * 3. 查询流量计：根据“流量计模型(新增)”的“流量计代码”和“所属公司编号”查询流量计“流量计_1”；(√) <br/>
     * 4. 判断流量计和DTU的存在性：如果“DTU_1”和“流量计_1”都存在，直接返回“DTU_1”和“流量计_1”的编号， <br/>
     *  如果“DTU_1”存在且“流量计_1”不存在，抛出异常“DTU代码已存在”， <br/>
     *  如果“流量计_1”存在且“DTU_1”不存在，抛出异常“流量计代码已存在”；(√) <br/>
     * 5. 创建DTU：根据“流量计模型(新增)”的信息在登录用户所在的物联网设备公司下创建DTU“DTU_2”；(√) <br/>
     * 6. 创建流量计：根据“流量计模型(新增)”的信息以及需要关联“DTU_2”和“燃气用户_1”来创建流量计“流量计_2”；(√) <br/>
     * 8. 返回信息：根据“流量计_2”和“DTU_2”生成“流量计响应模型(新增)”并返回；(√) <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see FlowMeterPostModel body <br/>
     * @see FlowMeterPostResponseModel result <br/>
     */ 
    public FlowMeterPostResponseModel post(Map<String, String> header, Map<String, Object> query, FlowMeterPostModel body) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(body, headers);

        Map<String, Object> uriVariables = new HashMap<>();

        try {
            ResponseEntity<FlowMeterPostResponseModel> result = restTemplate.exchange("http://device-service/flow-meters?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.POST, httpEntity, FlowMeterPostResponseModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("FLOWMETERSERVICE_POST_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("FLOWMETERSERVICE_POST_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

